<template>
	<view>
		<view class="product-window"
			:class="(attr.cartAttr === true ? 'on' : '') + ' ' + (iSbnt?'join':'') + ' ' + (iScart?'joinCart':'')">
			<view class="textpic acea-row row-between-wrapper">
				<view class="pictrue" @click="showImg()">
					<image :src="attr.productSelect.image"></image>
				</view>
				<view class="text">
					<view class="line1">
						{{ attr.productSelect.storeName }}
					</view>
					<view class="money">
						<view class="flex align-baseline">
							￥<text
								class="num">{{ attr.productSelect.daoshouprice!=null?attr.productSelect.daoshouprice: attr.productSelect.price }}</text>
							<text v-if="attr.productSelect.daoshouprice" style="padding-left: 10rpx;">券后价</text>
							<text class="stock" v-if="attr.productSelect.daoshouprice"
								style="padding-left: 10rpx;">￥{{attr.productSelect.price}} </text>
						</view>
					</view>
				</view>
				<view class="iconfont icon-guanbi" @click="closeAttr"></view>
			</view>
			<view class="shibie">
				{{useridimg}}
			</view>
			<view class="rollTop">
				<view class="productWinList">
					<view class="item" v-for="(item, indexw) in attr.productAttr" :key="indexw">
						<view class="title">{{ item.attrName }}</view>
						<view class="listn acea-row row-middle">
							<view class="itemn" :class="item.index === itemn ? 'on' : ''"
								v-for="(itemn, indexn) in item.attrValues" @click="tapAttr(indexw, indexn)"
								:key="indexn">
								<img :src="qhselect" v-if="item.index === itemn && tagData[indexw][indexn] === '0'"
									mode="widthFix" class="qhicon" alt="" />
								<img :src="qhnoselect" v-if="item.index !== itemn && tagData[indexw][indexn] === '0'"
									mode="widthFix" class="qhicon" alt="" />
								<!-- <span>{{ itemn }}</span> -->
								<text class="qh"
									:style="tagData[indexw][indexn] === '0' ? 'padding-left: 20rpx' : 'padding-left: 0rpx'">{{ itemn }}</text>
							</view>
						</view>
					</view>
				</view>
				<view class="cart acea-row row-between-wrapper">
					<view class="title">数量</view>
					<view class="carnum acea-row row-left">
						<view class="item reduce" :class="attr.productSelect.cart_num <= 1 ? 'on' : ''"
							@click="CartNumDes">
							-
						</view>
						<view class='item num'>
							<input type="number" v-model="attr.productSelect.cart_num"
								data-name="productSelect.cart_num" @input="bindCode(attr.productSelect.cart_num)"
								maxlength="3"></input>
						</view>
						<view v-if="iSplus" class="item plus" :class="
				      attr.productSelect.cart_num >= attr.productSelect.stock
				        ? 'on'
				        : ''
				    " @click="CartNumAdd">
							+
						</view>
						<view v-else class='item plus'
							:class='(attr.productSelect.cart_num >= attr.productSelect.quota) || (attr.productSelect.cart_num >= attr.productSelect.stock) || (attr.productSelect.cart_num >= attr.productSelect.num)? "on":""'
							@click='CartNumAdd'>+</view>
					</view>
				</view>
			</view>
			<view class="joinBnt bg_color" v-if="iSbnt && attr.productSelect.stock>0 &&attr.productSelect.quota>0"
				@click="goCat">我要参团</view>
			<view class="joinBnt on"
				v-else-if="(iSbnt && attr.productSelect.quota<=0)||(iSbnt &&attr.productSelect.stock<=0)">已售罄</view>
			<view class="joinBnt bg_color" v-if="iScart && attr.productSelect.stock" @click="goCat">确定</view>
			<!-- <view class="joinBnt bg-color" v-if="iSbnt && attr.productSelect.stock && attr.productSelect.quota" @click="goCat">确定</view> -->
			<view class="joinBnt on" v-else-if="(iScart && !attr.productSelect.stock)">已售罄</view>
		</view>
		<view class="mask" @touchmove.prevent :hidden="attr.cartAttr === false" @click="closeAttr"></view>
	</view>
</template>

<script>
	export default {
		props: {
			attr: {
				type: Object,
				default: () => {}
			},
			markQh: {
				type: Array,
				default: () => []
			},
			limitNum: {
				type: Number,
				value: 0
			},
			isShow: {
				type: Number,
				value: 0
			},
			iSbnt: {
				type: Number,
				value: 0
			},
			iSplus: {
				type: Number,
				value: 0
			},
			iScart: {
				type: Number,
				value: 0
			},
			useridimg: {
				type: String,
				value: '··········· ··········· ··········· ··········· ··········· ··········· ··········· ···········'
			}
		},
		data() {
			return {
				qhselect: 'https://pic.mae.vip//crmebimage/public/maintain/2024/07/24/21fa9f807795425da9381090f2f5873bycwft2kkzm.png',
				qhnoselect: 'https://pic.mae.vip//crmebimage/public/maintain/2024/07/24/24d3f9f284bd4db1ae8c8c3ba8b34d27n7mwbp439x.png',
				tagData: [],
				pValue: {},
				pAttr: {},
			};
		},
		watch: {
			markQh(newVal, oldVal) {
				// if (this.tagData.length === 0) {
				// 	this.tagData = JSON.parse(JSON.stringify(newVal));
				// 	console.log('!@#$------666 ' + JSON.stringify(this.tagData))
				// }
				if (newVal) {
					this.tagData = [];
					this.tagData = JSON.parse(JSON.stringify(newVal));
					console.log('!@#$------666 ' + JSON.stringify(this.tagData))
				}
			},
			attr(newVal, oldVal) {
				// if (Object.keys(this.pAttr).length === 0) {
				// 	this.pAttr = JSON.parse(JSON.stringify(newVal));
				// 	//console.log('!@#$------777' + JSON.stringify(this.pAttr))
				// }

				if (newVal) {
					this.pAttr = {};
					this.pAttr = JSON.parse(JSON.stringify(newVal));
					//console.log('!@#$------777' + JSON.stringify(this.pAttr))
				}
			},
		},
		methods: {
			goCat: function() {
				this.$emit('goCat');
			},
			/**
			 * 购物车手动输入数量
			 * 
			 */
			bindCode: function(e) {
				this.$emit('iptCartNum', this.attr.productSelect.cart_num);
			},
			closeAttr: function() {
				this.$emit('myevent');
			},
			CartNumDes: function() {
				this.$emit('ChangeCartNum', false);
			},
			CartNumAdd: function() {
				this.$emit('ChangeCartNum', true);
			},
			setPValue(value) {
				let vstring = JSON.stringify(value);
				vstring = vstring.replaceAll('.', '###');
				//console.log('_____________*****+++++++111 ' + vstring)
				//value = value.repl
				try {
					this.pValue = JSON.parse(vstring);
				} catch (e) {

				}

				//console.log('_____________*****+++++++111 ' + value)
				//console.log('_____________*****+++++++111 ' + this.pValue)

			},
			tapAttr: function(indexw, indexn) {

				let vstring = JSON.stringify(this.pValue);
				vstring = vstring.replaceAll('###', '.');
				try {
					this.pValue = JSON.parse(vstring);
				} catch (e) {

				}
				//console.log('666_____________*****+++++++111 ' + JSON.stringify(this.pValue))
				let that = this;
				that.$emit("attrVal", {
					indexw: indexw,
					indexn: indexn
				});
				this.$set(this.attr.productAttr[indexw], 'index', this.attr.productAttr[indexw].attrValues[indexn]);
				let value = that.getCheckedValue().join(",");
				//console.log('===+++' + JSON.stringify(value))
				that.$emit("ChangeAttr", value);

				return;
				//this.pValue = JSON.parse(JSON.stringify(this.attr.productAttr));


				let temporaryc = []; //中间临时变量
				let deepc = []; //深拷贝一份用
				for (var i = 0; i < that.pAttr.productAttr.length; i++) {
					deepc.push(that.pAttr.productAttr[i].attrValues);
				}
				//console.log('_____________*****+++++++111 ' + JSON.stringify(that.pValue))

				temporaryc = JSON.parse(JSON.stringify(deepc));


				//that.tagData = [];
				that.tagData = JSON.parse(JSON.stringify(deepc));
				//that.tagData = deepc;

				for (var i = 0; i < temporaryc.length; i++) {
					let abc = JSON.parse(JSON.stringify(that.getCheckedValue()));
					//console.log('_____________*****+++++++abc ' + JSON.stringify(abc))
					for (var j = 0; j < temporaryc[i].length; j++) {
						if (i === 0) { //第一排 
							let fff = 0;
							for (let k in that.pValue) {
								if (k.indexOf(temporaryc[i][j]) !== -1) {
									if (that.pValue[k]['stock'] > 0) {
										fff = 1;
									}
								}
							}
							if (fff === 1) { //不缺货
								that.tagData[i][j] = '1';
							} else { //缺货
								that.tagData[i][j] = '0';
							}
							//console.log('_____________*****' + JSON.stringify(that.tagData))
						} else { //下边的  只看默认选中的第一排下的  其他的需要切换的时候再去计算
							//return;
							let fff = 0;
							let sstr = abc.slice(0, i).join(','); //-------------------
							//console.log('_____________*****################123 ' + sstr)
							//console.log('_____________*****################456 ' + JSON.stringify(that.pValue))
							for (let k in that.pValue) {
								// if (k.indexOf(temporaryc[i][j]) !== -1 && k.indexOf(sstr) !== -1) {
								// 	//console.log('_____________*****################---+++' + k)
								// 	if (that.pValue[k]['stock'] > 0) {
								// 		fff = 1;
								// 	}
								// }
								if (k.split(',')[i] === temporaryc[i][j] && k.indexOf(sstr) !== -1) {
									//console.log('_____________*****################---+++' + k)
									if (that.pValue[k]['stock'] > 0) {
										fff = 1;
									}
								}
							}
							if (fff === 1) { //不缺货
								that.tagData[i][j] = '1';
							} else { //缺货
								that.tagData[i][j] = '0';
							}
						}
					}
				}
				//console.log('_____________*****@@@@@@@@@@@@@@@@@' + JSON.stringify(that.tagData))
				//that.$forceUpdate();
			},
			//获取被选中属性；
			getCheckedValue: function() {
				var that = this;
				var cc = [];
				let productAttr = this.attr.productAttr;
				let value = [];
				for (let i = 0; i < productAttr.length; i++) {
					for (let j = 0; j < productAttr[i].attrValues.length; j++) {
						if (productAttr[i].index === productAttr[i].attrValues[j]) {
							value.push(productAttr[i].attrValues[j]);
							cc.push(j);
						}
					}
				}
				that.$emit("ChangeAttrIndex", cc.join(","));
				return value;
			},
			showImg() {
				this.$emit('getImg');
			},
		}
	}
</script>

<style scoped lang="scss">
	.qh {
		width: 100%;
		text-align: center;
	}

	.qhicon {
		width: 70rpx;
		height: 30rpx;
		position: relative;
		float: right;
		margin-top: -10px;
		margin-right: -15px;
		background: white;
	}

	.product-window {
		position: fixed;
		bottom: 0;
		width: 100%;
		left: 0;
		background-color: #fff;
		z-index: 77;
		border-radius: 16rpx 16rpx 0 0;
		padding-bottom: 100rpx;
		padding-bottom: calc(env(safe-area-inset-bottom) + 100rpx);
		transform: translate3d(0, 100%, 0);
		transition: all .2s cubic-bezier(0, 0, .25, 1);
	}

	.product-window.on {
		transform: translate3d(0, 0, 0);
	}

	.product-window.join {
		padding-bottom: 30rpx;
	}

	.product-window.joinCart {
		padding-bottom: 30rpx;
		z-index: 999;
	}

	.product-window .textpic {
		padding: 0 130rpx 0 30rpx;
		margin-top: 29rpx;
		position: relative;
	}

	.product-window .textpic .pictrue {
		width: 150rpx;
		height: 150rpx;
	}

	.product-window .textpic .pictrue image {
		width: 100%;
		height: 100%;
		border-radius: 10rpx;
	}

	.product-window .textpic .text {
		width: 410rpx;
		font-size: 32rpx;
		color: #333333;
	}

	.product-window .textpic .text .money {
		font-size: 24rpx;
		margin-top: 23rpx;
		@include price_color(theme);
	}

	.product-window .textpic .text .money .num {
		font-family: PingFang SC;
		font-size: 36rpx;
		font-weight: 600;
	}

	.product-window .textpic .text .money .stock {
		color: #999;
	}

	.product-window .textpic .iconfont {
		position: absolute;
		right: 30rpx;
		top: -5rpx;
		font-size: 35rpx;
		color: #8a8a8a;
	}

	.product-window .rollTop {
		max-height: 760rpx; //规格较多时候弹框高度为高为800rpx
		overflow: auto;
		margin-top: 36rpx;
	}

	.product-window .productWinList .item~.item {
		margin-top: 36rpx;
	}

	.product-window .productWinList .item .title {
		font-size: 30rpx;
		color: #999;
		padding: 0 30rpx;
	}

	.product-window .productWinList .item .listn {
		padding: 0 30rpx 0 16rpx;
	}

	.product-window .productWinList .item .listn .itemn {
		border: 1px solid #F2F2F2;
		font-size: 26rpx;
		color: #282828;
		padding: 7rpx 33rpx;
		border-radius: 40rpx;
		margin: 20rpx 0 0 14rpx;
		background-color: #F2F2F2;
	}

	.product-window .productWinList .item .listn .itemn.on {
		@include main_color(theme);
		@include coupons_border_color(theme);
		@include cate-two-btn(theme);
	}

	.product-window .productWinList .item .listn .itemn.limit {
		color: #999;
		text-decoration: line-through;
	}

	.product-window .cart {
		margin-top: 50rpx;
		margin-bottom: 30rpx;
		padding: 0 30rpx;
	}

	.product-window .cart .title {
		font-size: 30rpx;
		color: #999;
	}

	.product-window .cart .carnum {
		height: 54rpx;
	}

	.product-window .cart .carnum view {
		// border: 1px solid #a4a4a4;
		width: 84rpx;
		text-align: center;
		height: 100%;
		line-height: 54rpx;
		color: #282828;
		font-size: 45rpx;
	}

	.product-window .cart .carnum .reduce {
		border-right: 0;
		border-radius: 6rpx 0 0 6rpx;
		line-height: 48rpx;
	}

	.product-window .cart .carnum .reduce.on {
		// border-color: #e3e3e3;
		color: #DEDEDE;
		font-size: 64rpx;
	}

	.product-window .cart .carnum .plus {
		border-left: 0;
		border-radius: 0 6rpx 6rpx 0;
		line-height: 46rpx;
	}

	.product-window .cart .carnum .plus.on {
		border-color: #e3e3e3;
		color: #dedede;
	}

	.product-window .cart .carnum .num {
		background: rgba(242, 242, 242, 1);
		color: #282828;
		font-size: 28rpx;
		border-radius: 12rpx;
		line-height: 29px;
		height: 54rpx;

		input {
			display: -webkit-inline-box;
		}
	}

	.product-window .joinBnt {
		font-size: 30rpx;
		width: 620rpx;
		height: 86rpx;
		border-radius: 50rpx;
		text-align: center;
		line-height: 86rpx;
		color: #fff;
		margin: 21rpx auto 0 auto;
	}

	.align-baseline {
		align-items: baseline;
	}

	.bg_color {
		@include main-bg_color(theme);
	}

	.product-window .joinBnt.on {
		background-color: #bbb;
		color: #fff;
	}

	.align-center {
		align-items: center;
	}

	.vip_icon {
		width: 44rpx;
		height: 28rpx;
	}

	.vip_money {
		background: #FFE7B9;
		border-radius: 4px;
		font-size: 22rpx;
		color: #333;
		line-height: 28rpx;
		text-align: center;
		padding: 0 6rpx;
		box-sizing: border-box;
		margin-left: -4rpx;
	}

	.pl-2 {
		padding-left: 20rpx;
	}

	.shibie {
		height: 10rpx;
		text-align: justify;
		text-align-last: justify;
		color: #cccccc;
		width: 100%;
		line-height: 10rpx;
		padding: 0rpx 30rpx;
	}
</style>